package com.project.common.domain;

import lombok.Data;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页响应封装
 *
 */
@Data
public class BizPage<D> extends BaseDoConvert {
    private Integer totalPages;
    private Integer currPage;
    private Integer number;
    private Integer size;
    private Long totalElements;
    private List<D> content = new ArrayList<>();

    public static <D> BizPage of(Page page, Class<D> dClass) {
        if (page.getContent().isEmpty()) {
            return empty(page);
        }
        return new BizPage(page, dClass);
    }

    public static <D> BizPage of(Pageable page, List<D> d) {
        BizPage bizPage = new BizPage();
        bizPage.setNumber(page.getPageNumber());
        bizPage.setSize(page.getPageSize());
        bizPage.setContent(d);
        return bizPage;
    }

    public BizPage() {
        this.currPage = 0;
        this.totalPages = 0;
        this.totalElements = 0L;
    }

    public BizPage(Page page, Class<D> dClass) {
        this.totalPages = page.getTotalPages();
        this.totalElements = page.getTotalElements();
        this.currPage = page.getNumber();
        this.size = page.getSize();
        this.number = page.getNumber();
        if (page.hasContent()) {
            page.getContent().forEach(po -> {
                D d = null;
                try {
                    d = dClass.getDeclaredConstructor().newInstance();
                    map(po, d);
                    this.content.add(d);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    /**
     * 对象需要自己转换
     *
     * @param page
     */
    public BizPage(Page page) {
        this.currPage = page.getNumber();
        this.totalPages = page.getTotalPages();
        this.totalElements = page.getTotalElements();
        this.content = page.getContent();
    }

    public BizPage(Page page, Integer pageNumber, Class<D> dClass) {
        this.currPage = pageNumber;
        this.totalPages = page.getTotalPages();
        this.totalElements = page.getTotalElements();

        if (page.hasContent()) {
            page.getContent().forEach(po -> {
                D d = null;
                try {
                    d = dClass.getDeclaredConstructor().newInstance();
                    map(po, d);
                    this.content.add(d);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            });
        }
    }


    public BizPage(List list, Class<D> dClass, Integer totalPages, Long totalElements, Integer currPage) {
        this.currPage = currPage;
        this.totalPages = totalPages;
        this.totalElements = totalElements;

        if (null != list && !list.isEmpty()) {
            list.forEach(po -> {
                D d = null;
                try {
                    d = dClass.getDeclaredConstructor().newInstance();
                    map(po, d);
                    this.content.add(d);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public BizPage(List list, Page page) {
        this.content = list;
        this.currPage = page.getNumber();
        this.totalPages = page.getTotalPages();
        this.totalElements = page.getTotalElements();

    }

    public static BizPage empty() {
        return new BizPage();
    }

    public static BizPage empty(Page page) {
        BizPage bizPage = new BizPage();
        bizPage.setCurrPage(page.getNumber());
        bizPage.setNumber(page.getNumber());
        bizPage.setSize(page.getSize());
        bizPage.setContent(new ArrayList<>());
        return bizPage;
    }

    public static BizPage empty(Pageable page) {
        BizPage bizPage = new BizPage();
        bizPage.setCurrPage(page.getPageNumber());
        bizPage.setNumber(page.getPageNumber());
        bizPage.setSize(page.getPageSize());
        bizPage.setContent(new ArrayList<>());
        return bizPage;
    }

}
